#include "Time.h"
#include <Windows.h>

namespace {
	__int64 timerFrequency;
	bool setup = false;

	void setupPerfTime(){
		LARGE_INTEGER t;
		QueryPerformanceFrequency(&t);
		timerFrequency = t.QuadPart;
		setup = true;
	}
}

namespace time {
	__int64 nanoTime(){
		if(!setup){
			setupPerfTime();
		}
		if(timerFrequency != 0) {
			LARGE_INTEGER timel;
			QueryPerformanceCounter(&timel);
			__int64 time = timel.QuadPart;
			return static_cast<__int64>(((double)time / (double)timerFrequency) * 1000000000.0);
		} else {
			return milliTime() * 1000000;
		}
	}

	__int64 milliTime(){
		return GetTickCount64();
	}
}